package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListDao damageListDao;

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    @Override
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr) {

        ArrayList<DamageListGoods> damageListGoodsList  =
                JSON.parseObject(damageListGoodsStr, new TypeReference<ArrayList<DamageListGoods>>(){});

        if (damageList.getDamageListId() != null) {
            //修改   只能改单价和数量
        }else {
            //保存
            damageListDao.insertdamageList(damageList);
            damageListGoodsList.stream().forEach(damageListGoods -> {
                damageListGoods.setDamageListId(damageList.getDamageListId());
                damageListGoodsDao.insertdamageListGoods(damageListGoods);
            });

        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        ArrayList<DamageList> damageList= damageListDao.list(sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", damageList);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        List<DamageListGoods> damageGoodsList = damageListGoodsDao.goodsListBydamageListId(damageListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", damageGoodsList);

        return map;
    }
}
